package dwl.graduation.design.config;


import dwl.graduation.design.job.CollectSysJob;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {
    @Bean //job配置
    JobDetailFactoryBean jobDetailFactoryBean(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setJobClass(CollectSysJob.class);
        return jobDetailFactoryBean;
    }


    @Bean  //调度器配置
    CronTriggerFactoryBean cronTriggerFactoryBean(){
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean().getObject());
        cronTriggerFactoryBean.setCronExpression("0/30 0/5 * * * ?");
        return cronTriggerFactoryBean;
    }

    @Bean
    public MyJobFactory jobFactory() {
        return new MyJobFactory();
    }


    @Bean  //中心配置
    SchedulerFactoryBean schedulerFactoryBean(){
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(cronTriggerFactoryBean().getObject());
        schedulerFactoryBean.setJobFactory(jobFactory());
        return schedulerFactoryBean;
    }
    @Bean(name="Scheduler")  //获得可以操作定时任务的对象
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }

}
